/**
 * Copyright (c) 2011, Todd Ginsberg
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * Neither the name of Todd Ginsberg, or Gowalla nor the
 *      names of any contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 *  Also, please use this for Good and not Evil.  
 */
package com.ginsberg.gowalla.example;

import com.ginsberg.gowalla.Gowalla;
import com.ginsberg.gowalla.auth.OAuth2Authentication;
import com.ginsberg.gowalla.dto.Button;
import com.ginsberg.gowalla.dto.CheckinReceipt;
import com.ginsberg.gowalla.dto.FullSpot;
import com.ginsberg.gowalla.dto.ReceiptComponent;
import com.ginsberg.gowalla.dto.User;
import com.ginsberg.gowalla.exception.GowallaException;

/**
 * An example of how to handle a CheckinReceipt. 
 *
 * @author Todd Ginsberg
 */
public class PrintCheckinReceipt {

	public static final String API_KEY = "<YOUR API KEY HERE>";
	public static final String AUTH_TOKEN = "<YOUR CLIENT READ_WRITE KEY HERE>";
	private Gowalla gowalla = null;
	
	/**
	 * Constructor!
	 */
	public PrintCheckinReceipt() {
		super();
		gowalla = new Gowalla("PrintCheckinReceipt", API_KEY, new OAuth2Authentication(AUTH_TOKEN));
	}
	
	public void checkin(final int spotId) throws GowallaException {
		final FullSpot spot = gowalla.getSpot(spotId);
		final CheckinReceipt receipt = gowalla.checkin(spotId, spot.getGeoLocation(), null, false, false, true);
		printReceipt(receipt);
	}
	
	private void printReceipt(final CheckinReceipt receipt) {
		final StringBuilder buf = new StringBuilder();
		buf.append("Receipt Id   : ").append(String.format("%d%n", receipt.getId()));
		buf.append("Checkin Date : ").append(String.format("%tc%n", receipt.getCreatedAt()));
		buf.append("   User Id   : ").append(String.format("%d%n", receipt.getUser().getId()));
		buf.append("      Spot   : ").append(String.format("%s, (#%d)%n", receipt.getSpot().getName(), receipt.getSpot().getId()));
		
		for(ReceiptComponent c : receipt.getComponents()) {
			buf.append("--------------------------------------------\n");
			buf.append("Type: " + c.getType() + "\n");

			for(String h : c.getHeadings()) {
				buf.append(String.format(" -- %s --%n", h));
			}
			if(c.getImageUrl() != null) {
				buf.append(String.format("Img<%s>%n", c.getImageUrl()));
			}
			if(c.getMessage() != null) {
				buf.append(String.format("%s%n", c.getMessage()));
			}
			switch(c.getType()) {
				case NOTE: {
					renderNoteDetail(c, buf);
					break;
				}
				case HIGHLIGHT: {
					renderHighlightDetail(c, buf);
					break;
				}
				case ITEM: {
					renderItemDetail(c, buf);
					break;
				}
				case LEADERBOARD: {
					renderLeaderboard(c, buf);
					break;
				}
			}
			for(Button b : c.getButtons()) {
				buf.append(String.format("[%s] (%s)%n", b.getText(), b.getUrl()));
			}
		}
		buf.append("--------------------------------------------\n");
		
		System.out.println(buf);
	}

	private void renderItemDetail(final ReceiptComponent c, final StringBuilder buf) {
		buf.append(String.format("Item Issue Number: %d%n", c.getItem().getIssueNumber()));
	}
	
	private void renderNoteDetail(final ReceiptComponent c, final StringBuilder buf) {
		buf.append(String.format("Note Left By: %s%n", c.getCreator().getName()));
	}
	
	private void renderHighlightDetail(final ReceiptComponent c, final StringBuilder buf) {
		buf.append(String.format("Highlight Left By: %s%n", c.getCreator().getName()));
	}
	
	private void renderLeaderboard(final ReceiptComponent c, final StringBuilder buf) {
		buf.append(String.format("Leaders for the last %d days:%n", c.getDurationInDays()));
		int users = 0;
		for(User u : c.getUsers()) {
			buf.append(String.format("   %d - %s%n", ++users, u.getName()));
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws GowallaException {
		final PrintCheckinReceipt p = new PrintCheckinReceipt();
		p.checkin(11888);
	}

}
